package notify

import (
	"context"
	"log"
	"math/rand"
	"time"
)

//演示 协程通知机制

//case1 , 一个协程 通知多个协程 【超时等待】

func UseContextCase() {
	ctx,cancel := context.WithCancel(context.Background())
	var randN  = rand.Intn(30)+1
	for i:=0;i<randN;i++ {
		go func() {
			for {
				select {
				case <-ctx.Done():
					log.Println("退出协程")
					return
				default:
					time.Sleep(1*time.Second)
					log.Println("waiting...")
				}

			}
		}()
	}


	log.Println(cancel)
	time.Sleep(time.Second*10)
	log.Println("main goroutine cancel")
	cancel()
	//
	time.Sleep(time.Second*20)
	log.Println("退出程序")
}

